package com.example.concurrent.chapter03;

import java.util.concurrent.TimeUnit;

/**
 * @author Create By Liuqz
 * @date 2018/11/20 16:57
 */
public class FlagThreadExit {

    static class MyTask extends Thread {
        private volatile boolean closed = false;


        @Override
        public void run() {
            System.out.println("I will start work");
            while (!closed && !isInterrupted()) {
                //working
            }
            System.out.println("I will be exiting");
        }

        public void close() {
            this.closed = true;
            this.interrupt();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyTask t = new MyTask();
        t.start();
        TimeUnit.SECONDS.sleep(5);
        System.out.println("System will be shutdown");
        t.close();
    }
}
